package com.teamall.web.tssy.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.BaseController;
import com.teamall.core.common.CommonEnum;
import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.web.tssy.db.po.TsSyResourcePO;
import com.teamall.web.tssy.service.ITsSyResourceService;
import com.teamall.web.tssy.vo.TsSyResourceTree;

/**
 * <Description>tssyresource管理 <br>
 * 
 * @author codeCreater <br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.tssy.controller <br>
 */

@Controller
@RequestMapping("/tssy/tsSyResource")
public class TsSyResourceController extends BaseController {

    private static final LogManager logger = LogManager.getLogger(TsSyResourceController.class);

    @Autowired
    private ITsSyResourceService tsSyResourceService;
    
    @RequestMapping("index")
    public String index(Model model) {
        // ///////
        // TODO 根据业务场景，进行条件分支跳转、设置页面默认值等

        // ///////

        return "/page/tssy";
    }

    @RequestMapping("queryRecordByPage")
    public ModelAndView queryRecordByPage(TsSyResourcePO record,
            Page page,HttpServletRequest request) throws BaseAppException {
    	logger.info("queryRecordByPage by record begin...record=[{0}]", record);
    	ModelAndView mv = new JModelAndView("tssy/tsSyResourceList",request);
    	//将dwz分页参数转成Page对象
//    	Page page = dwzToPage(dwzPage);
    	Page<TsSyResourcePO> resultPage = tsSyResourceService.selectByArgAndPage(record, page);
    	mv.addObject("resultPage", resultPage);
    	mv.addObject("record", record);
    	
    	return mv;
    }

    /**
     * 资源树页面
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("initTreePage")
    public ModelAndView initTreePage(HttpServletRequest request) throws BaseAppException {
    	ModelAndView mv = new JModelAndView("tssy/tsSyResourceList",request);
    	return mv;
    }
    
    /**
     * 加载树数据
     * @param record
     * @return
     */
    @RequestMapping(value="loadTreeData",method=RequestMethod.GET)
    @ResponseBody
    public List<TsSyResourceTree> loadTreeData(TsSyResourcePO record)throws BaseAppException {
    	TsSyResourcePO queryPo = new TsSyResourcePO();
    	List<TsSyResourcePO> list = tsSyResourceService.selectByArg(queryPo);
    	/*Map<String,TsSyResourcePO> map = new HashMap<String, TsSyResourcePO>();
    	if(!CollectionUtils.isEmpty(list)){
    		for(TsSyResourcePO po:list){
    			map.put(po.getId(), po);
    		}
    	}*/
    	List<TsSyResourceTree> treeList = queryTreeByPid(record.getPid(),list);
    	return treeList;
    }
    
    private List<TsSyResourceTree> queryTreeByPid(String pid,List<TsSyResourcePO> list){
    	List<TsSyResourceTree> treeList = new ArrayList<>();
		if(!CollectionUtils.isEmpty(list)){
			for(TsSyResourcePO po : list){
				if(po.getPid().equals(pid)){
					TsSyResourceTree tree = new TsSyResourceTree();
					tree.setId(po.getId());
					tree.setName(po.getResourceName());
					tree.setPid(po.getPid());
					tree.setChildren(queryTreeByPid(po.getId(),list));
					tree.setResourceType(po.getResourceType());
					treeList.add(tree);
				}
			}
		}
    	return treeList;
    }
    
    @RequestMapping("initAdd")
    public ModelAndView initAdd(TsSyResourcePO record,HttpServletRequest request) throws BaseAppException {
        logger.info("initAdd add begin...record=[{0}]", record);
        ModelAndView mv = new JModelAndView("tssy/tsSyResourceAdd", request);
        return mv;
    }
    
    @RequestMapping(value="add",method=RequestMethod.POST)
    public ModelAndView add(TsSyResourcePO record) throws BaseAppException {
        logger.info("add record begin...record=[{0}]", record);
        Timestamp tt=new Timestamp(new Date().getTime());
        record.setCreateDate(tt);
        record.setCreateBy(userId);
        record.setUpdateBy(userId);
        record.setUpdateTime(tt);
        tsSyResourceService.add(record);
        return ajaxDoneSuccess(this.operSuccess);
    }

    @RequestMapping(value="update",method=RequestMethod.PUT)
    public ModelAndView update(TsSyResourcePO record) throws BaseAppException {
        logger.info("update record begin...record=[{0}]", record);
        Timestamp tt=new Timestamp(new Date().getTime());
        record.setUpdateBy(userId);
        record.setUpdateTime(tt);
        tsSyResourceService.update(record);
        return ajaxDoneSuccess(this.operSuccess);
    }

    @RequestMapping(value = "delete",method=RequestMethod.DELETE)
    public ModelAndView delete(@RequestParam(value = "id",required = true) String id) throws BaseAppException {
        logger.info("delete record begin...id=[{0}]", id);
        tsSyResourceService.updateStatus(id);
        return ajaxDoneSuccess(operSuccess);
    }

    @RequestMapping("qryRecordInfo")
    public ModelAndView qryRecordInfo(@RequestParam(value = "id",
            required = true) String id,HttpServletRequest request) throws BaseAppException {
    	logger.info("qryRecordInfo by id begin...id=[{0}]", id);
    	ModelAndView mv = new JModelAndView("tssy/tsSyResourceAdd",request);
    	TsSyResourcePO record = tsSyResourceService.selectByPrimaryKey(id);
    	mv.addObject("record", record); 
    	return mv;  
    }

    @RequestMapping(value="qryRecordInfoForAjax",method=RequestMethod.GET)
    @ResponseBody
    public Map qryRecordInfoForAjax(@RequestParam(value = "id",
            required = true) String id) throws BaseAppException {
    	logger.info("qryRecordInfo by id begin...id=[{0}]", id);
    	Map map = new HashMap();
    	TsSyResourcePO record = tsSyResourceService.selectByPrimaryKey(id);
    	map.put("record", record); 
    	return map;  
    }
    
    @RequestMapping(value="saveRecord",method=RequestMethod.POST)
    @ResponseBody
    public Map saveRecord(TsSyResourcePO record,HttpServletRequest request, HttpServletResponse response) throws BaseAppException {
    	Map map = new HashMap();
    	Timestamp tt=new Timestamp(new Date().getTime());
    	if(StringUtils.isEmpty(record.getId())){
			 record.setCreateDate(tt);
			 record.setCreateBy(userId);
    	     record.setUpdateBy(userId);
    	     record.setUpdateTime(tt);
    		 record.setIsDel("F");
    		 tsSyResourceService.add(record);
    	}else{
   	     	record.setUpdateBy(userId);
   	     	record.setUpdateTime(tt);
    		 tsSyResourceService.update(record);
    	}
    	map.put("record", record); 
    	return map;  
    }
    
    @RequestMapping(value="querryAll",method=RequestMethod.GET)
    @ResponseBody
    public List<TsSyResourcePO> querryAll(TsSyResourcePO record) throws BaseAppException {
    	List<TsSyResourcePO> result = tsSyResourceService.selectByArg(record);
    	return result;
    }
    
    @RequestMapping(value="delRecord",method=RequestMethod.DELETE)
    @ResponseBody
    public Map delRecord(@RequestParam(value = "id",
            required = true) String id) throws BaseAppException  {
    	Map map = new HashMap();
    	tsSyResourceService.updateStatus(id);
    	TsSyResourcePO record = tsSyResourceService.selectByPrimaryKey(id);
    	map.put("record", record); 
    	return map;  
    }
}
